package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


@Service
@Slf4j
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckItemMapper checkItemMapper;


    @Override
    public void add(CheckItem checkitem) {
        log.debug("service:" + checkitem);
        Integer i = 0;
        if (checkitem != null) {
            i = checkItemMapper.add(checkitem);
            if (i > 0)
                return;
            throw new BusinessRuntimeException("数据库sql有问题");
        }
        throw new BusinessRuntimeException("后端数据没有获取到");
    }

    @Override
    public void delById(Integer id) {
        log.debug("service:" + id);
        Integer i = 0;
        if (id != null) {
            i = checkItemMapper.delete(id);
            if (i > 0)
                return;
        }
    }

    @Override
    public void edit(CheckItem checkitem) {
        log.debug("service:" + checkitem);
        Integer i = 0;
        if (checkitem != null) {
            i = checkItemMapper.update(checkitem);
            if (i > 0)
                return;
            throw new BusinessRuntimeException("数据库sql有问题");

        }
        throw new BusinessRuntimeException("后端数据没有获取到");

    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.debug("service:" + queryPageBean);
        PageHelper.startPage(queryPageBean.getPageNum(), queryPageBean.getPageSize());
        Page<CheckItem> checkItems = checkItemMapper.selectCheckItem(queryPageBean.getCondition());
        log.debug("service:" + checkItems);
        return new PageResult(checkItems.getTotal(), checkItems);
    }

    @Override
    public CheckItem findById(Integer id) {
        log.debug("Service:" + id);
        CheckItem checkItem = null;
        if (id != null)
            checkItem = checkItemMapper.selectById(id);
        return checkItem;
    }

//    private Boolean utils(Object object , String methodName) {
//        Integer i = 0;
//        if (object != null) {
//            i = checkItemMapper.add(checkitem);
//            if (i <= 0)
//                throw new BusinessRuntimeException("数据库sql有问题");
//        } else {
//            throw new BusinessRuntimeException("后端数据没有获取到");
//        }
//        return i > 0;
//    }
}
